//
//  introView.m
//  XMPPDemo
//
//  Created by Edison on 16/1/20.
//  Copyright © 2016年 Gnet. All rights reserved.
//

#import "GnetQM_introView.h"

@interface GnetQM_introView () <EAIntroDelegate,UIWebViewDelegate>
@property (nonatomic, strong) EAIntroView *introView;
@property (nonatomic, strong) UIViewController *topVC;
@property (nonatomic, strong) UIImageView *frontImageView;
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation GnetQM_introView

+ (instancetype)sharedView {
	static id instance;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		instance = [[self alloc] init];
	});
	return instance;
}

-(void)loadURLOrImages:(id)StringOrArray {
	NSLog(@"%@",[StringOrArray class]);
	if ([StringOrArray isKindOfClass:[NSArray class]]) {
		[self loadImage:StringOrArray];
	} else if ([StringOrArray isKindOfClass:[NSString class]]) {
		[self loadWebView:StringOrArray];
	}
}

#pragma mark- 网页引导页
-(void)loadWebView:(NSString *)URLString {
	//网页页面
	NSURL *URL = [NSURL URLWithString:URLString];
	NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:0 timeoutInterval:6];
	UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
	webView.scrollView.scrollEnabled = NO;
	webView.delegate = self;
	[webView loadRequest:request];
	[webView addSubview:self.frontImageView];
	self.webView = webView;
	
	//把网页放入引导页中
	EAIntroPage *page = [EAIntroPage pageWithCustomView:webView];
	self.introView.pages = @[page];
	self.introView.pageControl = nil;
	[self.introView setDelegate:self];
	self.introView.scrollView.scrollEnabled = NO;
	[self.introView showInView:self.topVC.view animateDuration:0.25];

}

#pragma mark- 图片引导页
-(void)loadImage:(NSArray *)ImageNamesArray {
	NSUInteger count = [ImageNamesArray count];
	NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
	for (int i = 0; i < count; i++) {
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
		imageView.image = [UIImage imageNamed:ImageNamesArray[i]];
		EAIntroPage *page = [EAIntroPage pageWithCustomView:imageView];
		page.bgImage = [UIImage imageNamed:ImageNamesArray[i]];
		[array addObject:page];
	}
	
	self.introView.pages = array;
	self.introView.scrollView.bounces = NO;
	[self.introView setDelegate:self];
	[self.introView showInView:self.topVC.view animateDuration:0.25];
}


-(EAIntroView *)introView {
		if (nil == _introView) {
			_introView = [[EAIntroView alloc] initWithFrame:[UIScreen mainScreen].bounds];
		}
	return _introView;
}

-(UIImageView *)frontImageView {
	if (nil == _frontImageView) {
		CGSize viewSize = [UIScreen mainScreen].bounds.size;
		NSString *viewOrientation = @"Portrait";    //横屏请设置成 @"Landscape"
		NSString *launchImage = nil;
		NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
		for (NSDictionary* dict in imagesDict)
			{
			CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
			
			if (CGSizeEqualToSize(imageSize, viewSize)
				&& [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
				launchImage = dict[@"UILaunchImageName"];
			}
		_frontImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
		_frontImageView.image = [UIImage imageNamed:launchImage];
	}
	return _frontImageView;
}

-(UIViewController *)topVC {
	if (nil == _topVC) {
		UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
		_topVC = appRootVC;
		if (_topVC.presentedViewController) {
			_topVC = _topVC.presentedViewController;
		}
	}
	return _topVC;
}

#pragma mark- 引导页代理方法
- (void)introDidFinish:(EAIntroView *)introView {
	
}
- (void)intro:(EAIntroView *)introView pageAppeared:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex {

	
}
- (void)intro:(EAIntroView *)introView pageStartScrolling:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex {
	
}
- (void)intro:(EAIntroView *)introView pageEndScrolling:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex {
	
}

#pragma mark- 网页代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	if ([request.URL.absoluteString isEqualToString:k_Introduction_ClickedButton_URL]
		|| navigationType == UIWebViewNavigationTypeLinkClicked) {
		[self.introView hideWithFadeOutDuration:0.25];
		return NO;
	}
	return  YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
	[[self.webView.subviews lastObject] removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error {
//	[self loadImage:@[@"bg1",@"bg2",@"bg3",@"bg4"]];
	[self.introView hideWithFadeOutDuration:0.25];

}

@end
